<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>退货管理首页</title>
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/color.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.easyui.min.js"></script>
</head>
<body>
	<table id="restock_dg" title="进货列表" class="easyui-datagrid"
		style="width: 100%; height: 480px"
		url="${param.request.contextPath}/restock/restocklist"
		toolbar="#restock_toolbar" pagination="true" rownumbers="true"
		fitColumns="true" singleSelect="true">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<!--  field字段需要与返回数据的属性名一致 -->
				<th field="restockId" align="center">退货单编号</th>
				<th field="carName" width="40">汽车名称</th>
				<th field="provider" formatter="providerFormatter" width="40">供应商</th>
				<th field="storage" formatter="storageFormatter" width="40">退货仓库</th>
				<th field="restockNum" width="40">退货数量</th>
				<th field="restockPrice" width="40">退货价格</th>
				<th field="restockDate" formatter="dateFormatter" width="150">退货日期</th>
				<th field="emp" formatter="empFormatter" width="40">处理人</th>
				<th field="remark" width="40">备注</th>
			</tr>
		</thead>
	</table>

	<div id="restock_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newRestock()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editRestock()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyRestock()">删除</a>


		<form id="ff" method="post">
			<div style="float: left;">
				<span>汽车名称：</span> <input class="easyui-textbox" type="text"
					name="carName" id="carName" data-options="validType:'carName'" />
				<span>供应商：</span> <input class="easyui-textbox" type="text"
					name="providerId" id="providerId"
					data-options="validType:'providerId'" /> <span>退货仓库：</span> <input
					class="easyui-textbox" type="text" name="storageId" id="storageId"
					data-options="validType:'storageId'" />
			</div>
			<div style="float: left;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-search"
					plain="true" onclick="doSearch1()">查询</a>
			</div>
		</form>
	</div>


	<div id="restock_dlg" class="easyui-dialog"
		style="width: 400px; height: 480px; padding: 10px 20px" closed="true"
		buttons="#restock_dlg-buttons">
		<div class="ftitle">信息管理</div>
		<form id="restock_fm" method="post">
			<div class="fitem">
				<label>汽车名称:</label> <input name="carName"
					class="easyui-validatebox" required="true">
			</div>
			<br>
			<div class="fitem">
				<label>&nbsp;供&nbsp;应&nbsp;商:</label> <input id="providerName"
					name="provider.providerId" class="easyui-combobox" required="true"
					data-options="valueField:'providerId',textField:'providerName',url:'${param.request.contextPath}/provider/findall'">
			</div>
			<br>
			<div class="fitem">
				<label>退货仓库:</label> <input id="storageName"
					name="storage.storageId" class="easyui-combobox" required="true"
					data-options="valueField:'storageId',textField:'storageName',url:'${param.request.contextPath}/storage/findall'">
			</div>
			<br>
			<div class="fitem">
				<label>退货数量:</label> <input name="restockNum"
					class="easyui-validatebox" required="true">
			</div>
			<br>
			<div class="fitem">
				<label>退货价格:</label> <input name="restockPrice"
					class="easyui-validatebox" required="true">
			</div>
			<br>
			<div class="fitem">

				<label>退货日期:</label> <input name="restockDate"
					class="easyui-datebox" required="true"></input>
			</div>
			<br>
			<div class="fitem">
				<label>&nbsp;处&nbsp;理&nbsp;人&nbsp;:</label> <input id="empName"
					name="emp.empId" class="easyui-combobox" required="true"
					data-options="valueField:'empId',textField:'empName',url:'${param.request.contextPath}/emp/findall'">
			</div>
			<br>
			<div class="fitem">
				<label>&nbsp;备&nbsp;&nbsp;&nbsp;&nbsp;注&nbsp;:</label> <input
					name="remark" class="easyui-validatebox" required="true">
			</div>
		</form>
	</div>

	<div id="restock_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="save()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#restock_dlg').dialog('close')">取消</a>
	</div>

	<script type="text/javascript">
		function providerFormatter(value, row, index) {
			return row.provider.providerName;
		}

		function storageFormatter(value, row, index) {
			return row.storage.storageName;
		}

		function empFormatter(value, row, index) {
			return row.emp.empName;
		}

		function dateFormatter(value, row, index) {
			var date = new Date(value);
			var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
		}

		function loadProvider(provider) {
			var data = $("#providerName").combobox("getData");
			if (data && data.length > 0) {
				if (provider == 0) {
					$("#providerName").combobox("setValue", data[0].providerId);
				} else {
					$("#providerName").combobox("setValue", provider);
				}
			}
		}
		
		function loadStorage(storage) {
			var data = $("#storageName").combobox("getData");
			if (data && data.length > 0) {
				if (storage == 0) {
					$("#storageName").combobox("setValue", data[0].storageId);
				} else {
					$("#storageName").combobox("setValue", storage);
				}
			}
		}

		function loadEmp(emp) {
			var data = $("#empName").combobox("getData");
			if (data && data.length > 0) {
				if (emp == 0) {
					$("#empName").combobox("setValue", data[0].empId);
				} else {
					$("#empName").combobox("setValue", emp);
				}
			}
		}

		function newRestock() {
			$('#restock_dlg').dialog('open').dialog('setTitle', '新增管理');
			$('#restock_fm').form('clear');
			url = '${pageContext.request.contextPath}/restock/add';
		}

		function editRestock() {
			$('#restock_fm').form('clear');
			var row = $('#restock_dg').datagrid('getSelected');
			if (row) {
				$('#restock_dlg').dialog('open').dialog('setTitle', '修改管理');
				$('#restock_fm').form('load', row);
				url = '${pageContext.request.contextPath}/restock/edit?restockId='
						+ row.restockId;
			}
			loadProvider(row.provider.providerId);
			loadStorage(row.storage.storageId);
			loadEmp(row.emp.empId);
		}

		function save() {
			$('#restock_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (!result.success) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$.messager.show({
							title : 'Info',
							msg : result.errorMsg
						});
					}

					$('#restock_dlg').dialog('close');
					$('#restock_dg').datagrid('reload');
				}
			});
		}

		function destroyRestock() {
			var row = $('#restock_dg').datagrid('getSelected');
			if (row) {
				$.messager
						.confirm(
								'Confirm',
								'确认删除?',
								function(r) {
									if (r) {
										$
												.post(
														'${pageContext.request.contextPath}/restock/delete',
														{
															restockId : row.restockId
														},
														function(result) {
															if (result.success) {
																$('#restock_dg')
																		.datagrid(
																				'reload');
															} else {
																$.messager
																		.show({
																			title : 'Error',
																			msg : result.errorMsg
																		});
															}
														}, 'json');
									}
								});
			}
		}

		function doSearch1() {
			$('#restock_dg').datagrid('load', {
				carName : $('#carName').textbox('getValue'),
				providerId : $('#providerId').textbox('getValue'),
				storageId : $('#storageId').textbox('getValue')
			});
			url = '${pageContext.request.contextPath}/restock/restocklist';
		}
	</script>


</body>
</html>